Imports Microsoft.VisualBasic
Imports SubSonic
Imports ClubStarterKit.Data
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports Shared_Routines
Imports System.IO
Imports System.Web

Namespace ClubStarterKit.Web
    Public Class Photos

#Region "Image Tasks"
        <System.ComponentModel.DataObjectMethodAttribute _
                (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function LoadImage(ByVal albumid As Integer, ByVal pageindex As Integer) As IDataReader
            Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()
            If imageFileLocation = ImageUtils.ImageLocation.Database Then
                Dim qry As New Query(Tables.Images)
                qry.QueryType = QueryType.Select
                qry.AddWhere(Images.Columns.Album, albumid)
                qry.OrderBy = OrderBy.Asc(Images.Columns.Id)
                qry.SelectList = Images.Columns.Id & "," & Images.Columns.Title & "," & Images.Columns.Notes
                qry.PageSize = "1"
                qry.PageIndex = pageindex
                Dim rdr As IDataReader = qry.ExecuteReader()
                Return rdr
                rdr.Close()
            Else
                Dim qry As New Query(Tables.Image)
                qry.QueryType = QueryType.Select
                qry.AddWhere(Image.Columns.Album, albumid)
                qry.OrderBy = OrderBy.Asc(Image.Columns.Id)
                qry.PageSize = "1"
                qry.PageIndex = pageindex
                qry.SelectList = Image.Columns.Id & "," & Image.Columns.Title & "," & Image.Columns.Notes
                Dim rdr As IDataReader = qry.ExecuteReader()
                Return rdr
                rdr.Close()
            End If
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
                (System.ComponentModel.DataObjectMethodType.Select, False)> _
                Public Shared Function LoadImages(ByVal albumid As Integer) As IDataReader
            Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()
            If imageFileLocation = ImageUtils.ImageLocation.Database Then
                Dim qry As New Query(Tables.Images)
                qry.QueryType = QueryType.Select
                qry.AddWhere(Images.Columns.Album, albumid)
                qry.OrderBy = OrderBy.Asc(Images.Columns.Id)
                qry.SelectList = Images.Columns.Id & "," & Images.Columns.Title & "," & Images.Columns.Notes
                Dim rdr As IDataReader = qry.ExecuteReader()
                Return rdr
                rdr.Close()
            Else
                Dim qry As New Query(Tables.Image)
                qry.QueryType = QueryType.Select
                qry.AddWhere(Image.Columns.Album, albumid)
                qry.OrderBy = OrderBy.Asc(Image.Columns.Id)
                qry.SelectList = Image.Columns.Id & "," & Image.Columns.Title & "," & Image.Columns.Notes
                Dim rdr As IDataReader = qry.ExecuteReader()
                Return rdr
                rdr.Close()
            End If
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
                (System.ComponentModel.DataObjectMethodType.Delete, False)> _
        Public Shared Sub DeleteImage(ByVal id As Integer)
            Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()
            If imageFileLocation = ImageUtils.ImageLocation.Database Then
                Dim qry As New Query(Tables.Images)
                qry.QueryType = QueryType.Delete
                qry.AddWhere(Images.Columns.Id, id)
                qry.Execute()
            Else
                Dim qry As New Query(Tables.Image)
                qry.QueryType = QueryType.Delete
                qry.AddWhere(Image.Columns.Id, id)
                qry.Execute()
            End If
        End Sub

        <System.ComponentModel.DataObjectMethodAttribute _
                (System.ComponentModel.DataObjectMethodType.Update, False)> _
                Public Shared Sub UpdateImage(ByVal title As String, ByVal notes As String, ByVal id As Integer)
            Try
                Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()
                If imageFileLocation = ImageUtils.ImageLocation.Database Then
                    Dim qry As New Query(Tables.Images)
                    qry.QueryType = QueryType.Update
                    qry.AddWhere(Images.Columns.Id, id)
                    If Not notes Is Nothing AndAlso Not notes = "" Then
                        qry.AddUpdateSetting(Images.Columns.Notes, notes)
                    Else
                        qry.AddUpdateSetting(Images.Columns.Notes, " ")
                    End If
                    qry.AddUpdateSetting(Images.Columns.Title, title)
                    qry.Execute()
                Else
                    Dim qry As New Query(Tables.Image)
                    qry.AddWhere(Image.Columns.Id, id)
                    qry.AddUpdateSetting(Image.Columns.Title, title)
                    If Not notes Is Nothing AndAlso Not notes = "" Then
                        qry.AddUpdateSetting(Image.Columns.Notes, notes)
                    Else
                        qry.AddUpdateSetting(Image.Columns.Notes, " ")
                    End If
                    qry.QueryType = QueryType.Update
                    qry.Execute()
                End If
            Catch ex As Exception
                MsgBox("Error updating image details")
            End Try
        End Sub

        Public Shared Function NumPages(ByVal albumid As Integer) As Integer
            Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()
            If imageFileLocation = ImageUtils.ImageLocation.Database Then
                Dim qry As New Query(Tables.Images)
                qry.AddWhere(Images.Columns.Album, albumid)
                Return qry.GetRecordCount()
            Else
                Dim qry As New Query(Tables.Image)
                qry.AddWhere(Image.Columns.Album, albumid)
                Return qry.GetRecordCount()
            End If
        End Function
#End Region

#Region "Album Tasks"
        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function SelectAlbumDetails(ByVal albumid As Integer) As SqlDataReader
            Dim qry As New Query(Tables.Albums)
            qry.QueryType = QueryType.Select
            qry.AddWhere(Albums.Columns.Albumid, albumid)
            qry.SelectList = Albums.Columns.Title & "," & Albums.Columns.Ownerid & "," & Albums.Columns.Isprivate & "," & Albums.Columns.Description & "," & Albums.Columns.Albumid
            Dim rdr As SqlDataReader = qry.ExecuteReader()
            Return rdr
            rdr.Close()
        End Function

        Public Shared Sub DeleteAlbum(ByVal albumid As Integer)
            Dim qry As New Query(Tables.Albums)
            qry.QueryType = QueryType.Delete
            qry.AddWhere(Albums.Columns.Albumid, albumid)
            qry.Execute()
        End Sub

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Update, False)> _
        Public Shared Sub UpdateAlbum(ByVal title As String, ByVal isprivate As Boolean, ByVal description As String, ByVal albumid As Integer)
            Dim qry As New Query(Tables.Albums)
            qry.QueryType = QueryType.Update
            qry.AddWhere(Albums.Columns.Albumid, albumid)
            qry.AddUpdateSetting(Albums.Columns.Title, title)
            qry.AddUpdateSetting(Albums.Columns.Isprivate, isprivate)
            If Not description Is Nothing AndAlso Not description = "" Then
                qry.AddUpdateSetting(Albums.Columns.Description, description)
            Else
                qry.AddUpdateSetting(Albums.Columns.Description, " ")
            End If
            qry.Execute()
        End Sub

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function SelectAlbumList(ByVal pagenum As Integer) As IDataReader
            If Settings.StoreImagesOnFile = True Then
                If HttpContext.Current.User.Identity.IsAuthenticated = True Then
                    Return SPs.PagedAlbumList1(Membership.GetUser().ProviderUserKey(), pagenum, Settings.PageSize).GetReader()
                Else
                    Return SPs.PagedAlbumList1(Guid.Empty, pagenum, Settings.PageSize).GetReader()
                End If
            Else
                If HttpContext.Current.User.Identity.IsAuthenticated = True Then
                    Return SPs.PagedAlbumList(Membership.GetUser().ProviderUserKey(), pagenum, Settings.PageSize).GetReader()
                Else
                    Return SPs.PagedAlbumList(Guid.Empty, pagenum, Settings.PageSize).GetReader()
                End If
            End If
        End Function

        Public Shared Function NumPages() As Integer
            Dim qry As New Query(Tables.Albums)
            Return Shared_Routines.NumPages(qry.GetRecordCount())
        End Function


#End Region

#Region "Temp Photo Tasks"

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Delete, False)> _
        Public Shared Sub DeleteTempImage(ByVal id As Integer)
            Dim item As New TempImagesFile(TempImagesFile.Columns.Id, id)

            Dim f As New FileInfo(HttpContext.Current.Request.PhysicalApplicationPath & "TempUpload\" & item.Filename)
            f.Delete()

            Dim thumbf As New FileInfo(HttpContext.Current.Request.PhysicalApplicationPath & "TempUpload\tmb" & item.Filename)
            thumbf.Delete()

            ClubStarterKit.Data.TempImagesFile.Delete(TempImagesFile.Columns.Id, item.Id)
        End Sub

        Public Shared Sub DeleteTempImage(ByVal item As TempImagesFile)
            Dim f As New FileInfo(HttpContext.Current.Request.PhysicalApplicationPath & "tempupload\" & item.Filename)
            f.Delete()

            Dim thumbf As New FileInfo(HttpContext.Current.Request.PhysicalApplicationPath & "tempupload\tmb" & item.Filename)
            thumbf.Delete()

            ClubStarterKit.Data.TempImagesFile.Delete(TempImagesFile.Columns.Id, item.Id)
        End Sub

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function LoadTempImages(ByVal albumid As Integer) As IDataReader
            Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()

            If imageFileLocation = ImageUtils.ImageLocation.Database Then
                Return TempImagesDB.FetchByParameter(TempImagesDB.Columns.Album, albumid)
            Else
                Return TempImagesFile.FetchByParameter(TempImagesFile.Columns.Album, albumid)
            End If
        End Function

#End Region

    End Class
End Namespace